IF EXISTS (SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('PaintingRelated','P'))
    DROP PROCEDURE PaintingRelated
GO
/*
 * Designer:     Kevin
 * Description:    
 * Created:      03/10/2011
 * History:
 * =============================================================================
 * Author      DateTime        Alter Description
 * =============================================================================
 */

CREATE PROCEDURE PaintingRelated
(
	@PaintingId int
)
AS 
BEGIN
	select top 8
		p2.painting_id as PaintingId,
		p2.Title,
		p2.image_url as ImageUrl,
		p2.painting_number as PaintingNumber
	from
	painting p
	inner join painting_subject ps on p.painting_id=@PaintingId and p.painting_id=ps.painting_id
	inner join painting_subject ps2 on ps.subject_id=ps2.subject_id and ps.painting_id<>ps2.painting_id
	inner join painting p2 on ps2.painting_id=p2.painting_id
END
GO